#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qtreewidget.h>
#include <qlistwidget.h>
#include <qtabbar.h>
#include <qtabwidget.h>
#include <qsplitter.h>
#include <qlabel.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <qthread.h>
#include <Qsci/qscidocument.h>
#include <QIcon>
#include <QColor>
#include <QTimer>

#include "VWords.h"
#include "VScintilla.h"
#include "VScintillaEx.h"
#include "VQscilexerCppAttach.h"
#include "VFunctions.h"
#include "VBackgroundSystem.h"
#include "VOutputInformation.h"
#include "VSearchDlg.h"
#include "VTabData.h"
#include "VStartPage.h"
#include "VProjectTreeWidget.h"
#include "VErrorWidget.h"
#include "VDockWidget.h"
#include "VOutputWidget.h"
#include "VOutlineWidget.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void signal_update_file_text(QString path, QString text);
    void signal_jump_request(QString path, QString pre, QString next, int line_number, bool in_function);
    void signal_build(QString qms_path);
    void signal_set_file_last_time(QString path, uint time);
    void signal_reload_testing();
    void signal_file_calltip(QString path, QString input);
    void signal_reload_file(QString path);
    void signal_rename(QString old_path, QString new_path);
    void signal_select_word(QString path, VWords *word);

    void signal_update_file_last_modify_time(QString path, uint time);
    void signal_file_tip(QString path, QString text);
    void signal_input_tip(QString path, QString text, int line, bool in);
    void signal_patameter_tip(QString path, QString name, int cursor_pos, int parameter_pos);
    void signal_mouse_hover_tip(QString path, QString before, QString after, int line, bool in, int pos);
    void signal_open_project(QString path);
    void signal_close_projct(QString path);
    void signal_dock_lock(int width, int height);
    void signal_dock_unlock();
    void signal_set_title(QString text, int time);

private slots:
    void slot_show_input_calltip(QList<VWords*> list);
    void slot_show_function_parameter_calltip(QString text, int pos);
    void slot_show_mouse_hover_calltip(QString text, int pos);
    void slot_jump(QString path, int line_number);
    void slot_show_build_information(QList<VErrorWidget::VTableItem> list);
    void slot_append_output_text(QString text);
    void slot_startpage_action(int type, QString text);
    void slot_change_symbol(QString text);
    void slot_save_file(QString path, QString text, int type, uint last);
    void slot_open_file(QString path);
    void slot_update_keywords(QString path, int type, QStringList list);
    void slot_rename(QString old_path, QString new_path);

    void slot_update_file_text(QString path, QString text);
    void slot_update_file_last_modify_time(QString path, uint time);
    void slot_reload_file(QString path);
    void slot_file_tip(QString path, QString text);
    void slot_input_tip(QString path, QString text, int line, bool in);
    void slot_patameter_tip(QString path, QString name, int cursor_pos, int parameter_pos);
    void slot_mouse_hover_tip(QString path, QString before, QString after, int line, bool in, int pos);
    void slot_hide_tip(int mode);
    void slot_open_project(QString path);
    void slot_select_word(QString path, VWords *word);
    void slot_jump_request(QString path, QString pre, QString next, int line_number, bool in_function);
    void slot_input_word_transfer(VWords *word);
    void slot_append_navigation(QString path, int line, int pos);
    void slot_scintilla_update_info(QString path, int line, int pos, int lines);
    void slot_find(const QString &expr, bool re, bool cs, bool wo,
                   bool wrap, bool forward, int line, int index,
                   bool show, bool posix, bool cxx11, QString rexpr,
                   bool replace,
                   bool replace_all);

private:
    Ui::MainWindow *ui;

    enum CloseStatus
    {
        CANCEL,
        SUCCESS,
        ERROR
    };

    struct NavigationInfo
    {
        int line;
        int pos;
        QString path;
    };

    struct VSctinillaStyle
    {
        int fore;
        int back;
        bool italics;
        bool bold;
    };
    QString _current_path;
    int _navigation_index = -1;
    QList<NavigationInfo> _navigation_list;
    void append_navigation(QString path, int line, int pos = -1);
    bool jump_to(QString path, int line, int pos=-1, bool append=true);

    QList<VDockWidget*> _dockwidget_list;
    VStartPage *_startpage_widget;
    QMenu *_content_menu;
    QMenu *_append_menu;
    QLabel *_line_label;
    QLabel *_column_label;
    QLabel *_sum_line_label;
    QLabel *_movie_label;
    QLabel *_message_label;
    QThread * _background_thread;
    VBackgroundSystem *_background_system;
    QLabel *_document_info_label;
    QLabel *_bbs_label;
    QMenu *_menubar_project;
    QMenu *_menubar_edit;
    QMenu *_menubar_search;
    QMenu *_menubar_biulder;
    QMenu *_menubar_tool;
    QMenu *_menubar_help;
    QTimer _message_timer;
    QToolBar *_statusbar_toolbar;
    VDockWidget *dockwidget=nullptr;
    QTimer _unlock_timer;
    VDockWidget *_project_dock;
    VProjectTreeWidget *_project_treewidget;
    VDockWidget *_output_dock;
    VOutputWidget * _output_widget;
    VDockWidget *_error_dock;
    VErrorWidget *_error_widget;
    VDockWidget *_startpage_dock;
    VSearchDlg *_search_widget;
    QDockWidget *_search_dock;
//    VOutlineWidget *_outline_widget;
//    VDockWidget *_outline_dock;

    bool apped_historical_record(QString path);     // 添加历史记录
    void init_qss();                                // 初始化样式
    void on_action_advance_triggered();             // 前进
    void on_action_retreat_triggered();             // 后退
    void on_action_new_project_triggered();         // 新建工程
    void on_action_open_project_triggered();        // 打开工程
    void on_action_close_file_triggered();          // 关闭文件
    void on_action_save_triggered();                // 保存
    void on_action_save_all_triggered();            // 保存所有
    void on_action_exit_triggered();                // 退出
    void on_action_undo_triggered();                // 撤销
    void on_action_redo_triggered();                // 重复
    void on_action_cut_triggered();                 // 剪切
    void on_action_copy_triggered();                // 拷贝
    void on_action_paste_triggered();               // 粘贴
    void on_action_comment_triggered();             // 注释
    void on_action_uncomment_triggered();           // 取消注释
    void on_action_left_ident_triggered();          // 左缩进
    void on_action_right_ident_triggered();         // 右边缩进
    void on_action_biulder_triggered();             // 生成
    void on_action_delete_triggered();              // 删除
    void on_action_close_project_triggered();       // 关闭工程
    void on_action_search_triggered();              // 搜索
    void on_action_new_file();                      // 新建文件
    void on_action_get_more();                      // 获取更多
    void on_action_rename();                        // 重命名
    void on_action_export_file();                   // 导出文件
    void on_action_open_to_dir();                   // 打开到文件夹
    void on_action_access_file_homepage();          // 访问文件主页
    void on_action_access_project_homepage();       // 访问项目主页
    void on_action_project_pack();                  // 项目打包
    void on_action_code_format();                   // 代码排版
    void on_action_project_attribute();             // 工程属性
    void on_action_startpage_trggered();            // 起始页
    void on_action_project_build_trggered();        // 生成
    void on_action_jump_trggered();                 // 快速跳转
    void on_action_foldAll_trggered();              // 折叠全部
    void on_action_expandedAll_trggered();          // 展开全部
    void on_action_search_pre_trggered();           // 搜索上一个
    void on_action_search_next_trggered();          // 搜索下一个
    void on_action_search_replace_trggered();       // 替换
    void on_action_search_replace_all_trggered();   // 全部替换
    void on_action_questions_trggered();            // 论坛
    void on_source_content_trggered();              // 编译信息
    void on_action_setting_trggered();              // 设置

    void init();
    void init_ui();

    void init_output_dock();
    void init_error_dock();
    void init_search_dock();
    void init_project_dock();
    void init_title();
    void init_call_tip_widget();
    void init_main_toolbar();
    void init_menubar();
    QList<QAction*> m_tool_actoin_list;
    void init_tool_menu();
    void init_statuebar();
    void init_background_system();
    void init_startpage_widget();
    void init_icon_path();
    void init_action();
    void init_action_icon();
    void init_action_shortCut();
    void init_qscintilla();
    void init_dup_qscintilla();
    void init_event_install();
    void init_content_menu();
    void init_append_menu();
    void set_document_info_label_text(int row, int col, int rows);
    bool _reload_testing = false;
    bool _close_save_testing = false;
    void reload_testing();
    void reload_file(QString path);
    QString get_file_text(QString path);
    bool build_project(QString path);
    QString get_calltip_icon(int type);
    QString calltip_item_text(VWords* p_word);
    VDockWidget *get_first_scintilla_dockwidget();
    VDockWidget* get_scintilla_dockwidget(QString path);
    VScintillaEx* get_scintilla(QString path);
    bool is_open_file(QString path);
    void open_file(QString path);
    void change_to_file(QString path);
    void scintilla_connect(VScintillaEx* scintilla);
    VScintillaEx* get_current_active_scintilla();
    void init_lock_timer();
    void init_start_param();
//    void init_outline_dock();
    // overload
    bool eventFilter(QObject *watched, QEvent *event);
    void readSettings();
    void closeEvent(QCloseEvent *event);
    void timerEvent(QTimerEvent *event);
};

Q_DECLARE_METATYPE(QList<VWords*>);
Q_DECLARE_METATYPE(QList<QTreeWidgetItem*>);
Q_DECLARE_METATYPE(QList<VErrorWidget::VTableItem>);

#endif // MAINWINDOW_H
